package sisicotren.ui.windows;

import java.util.concurrent.TimeUnit;

import javax.swing.JLabel;

import sisicotren.model.simulator.SimulationTimeOptions;

public class ClockPainter implements Runnable {

	public ClockPainter(JLabel targetLabel,SimulationTimeOptions timeSimul) {
		clock = targetLabel;
		simulationTime = timeSimul;
	}
	
	@Override
	public void run() {
		isRun = true;
		long init = System.currentTimeMillis();
		while(isRun) {
			long ms = System.currentTimeMillis();
			// reloj de la simulacion.
			long msSimu = (long) ((ms - init) * (60.00 / simulationTime.time()));
			long horas = TimeUnit.HOURS.convert(msSimu, TimeUnit.MILLISECONDS);
			long mins = TimeUnit.MINUTES.convert(msSimu, TimeUnit.MILLISECONDS);
			long segs = TimeUnit.SECONDS.convert(msSimu, TimeUnit.MILLISECONDS);
			String info = "" + (horas < 10 ? "0" + horas : horas) + ":" + 
				(mins % 60 < 10 ? "0" + mins % 60 : mins % 60 ) + ":" +
				(segs % 60 < 10 ? "0" + segs % 60 : segs % 60);
			this.clock.setText(info);
			
			Thread.yield();
		}
	}
	
	public void stop() {
		this.isRun = false;
	}

	private boolean isRun = false;
	private JLabel clock;
	private SimulationTimeOptions simulationTime;
}
